// yitai.js

var selected_style = {
    // backgroundColor: "#ddd",
    // color: "rgb(0,40,244)",
    color: "#40BBDD",
    fontWeight:"bold",
    borderBottom: "5px dotted #40BBDD"
};

var unselected_style = {
    color: "black",
    fontWeight: "normal",
    borderBottom: "0px",
};

$(document).ready(function(){
    /*
    $(document).find('[@showoption]').each(function(){
        var namevalue;
        var option={control:'',width:'400',height:'400',title:'',parent:null};
        var properties = $(this).attr('showoption').split(';'); 
        for(i=0;i<properties.length;i++)
        {
            namevalue = properties[i].split(':');
            if(namevalue.length >1)
            {
                execute ="option." + namevalue[0] +'=\''+ namevalue[1]+'\';';
                eval(execute);
            }
        }
        
        $(this).click(function(){
            ShowMessageBox(option);
            document.body.focus();
        });
    });
*/
    
});

function foldit(id){
    menu = $("#menu_"+id);
    submenu = $("#submenu_"+id);
    
    if($(submenu).css("visibility") == "visible"){
        submenu.hide("fast");
        submenu.css("visibility","hidden");
        // menu.css("background-image", "url('')");
        menu.css("font-weight", "bold");
    }else{
        submenu.show("fast");
        submenu.css("visibility","visible");
        // menu.css("background-image", "url('')");
        menu.css("font-weight", "normal");        
    }
}

function do_defense(id){
    cmd = $(":checked").val();
    // if(! cmd) return false;
    // alert(cmd);
    $.post("do_defense",
           {
               devid: ""+id+"",
               defense: ""+cmd+""
           },
           function(data){
               $("#result").html(data).show("slow").fadeOut(6000);
           }
          );
}

function do_add_user(){
    v_username = $("#username").val();
    v_password = $("#password").val();
    $.post("do_add_user",
          {
              username: ""+v_username+"",
              password: ""+v_password+""
          },
           function(data){
               $("#result").html(data).show("slow");
           }
          );
}

function dev_action(id){
    $("#dev-action").html("<span style='color:red'>loading...</span>");
    $.post("defense",
          {
              devid: ""+id+"",
          },
           function(data){
               $(".list ul").children().css(unselected_style);
               $("#item-"+id).css(selected_style);               
               $("#dev-action").html(data).show("slow");
           }
          );
}

function dev_status(id){
    $("#dev-status").html("<span style='color:red'>loading...</span>");
    $.post("status",
          {
              devid: ""+id+"",
          },
           function(data){
               $(".list ul").children().css(unselected_style);               
               $("#item-"+id).css(selected_style);
               $("#dev-status").html(data).show("slow");
           }
          );    
}

function chk_warn(id, phone, wkey){
    $the = $("#the_"+id+"_"+phone+"_"+wkey);
    $chk = $("#chk_"+id+"_"+phone+"_"+wkey);
    $btn = $("#btn_"+id+"_"+phone+"_"+wkey);
    if($chk.attr("checked")){        
        // add
        $.post("do_mod",
               {
                   devid: ""+id+"",
                   warn: ""+wkey+"",
                   mobile: ""+phone+"",
                   action: "active"
               },
               function(data){
                   $the.val(data);
                   $the.removeAttr("disabled");
                   $btn.removeAttr("disabled");                   
               }
              );
    }else{
        // remove
        $.post("do_mod",
               {
                   devid: ""+id+"",
                   warn: ""+wkey+"",
                   mobile: ""+phone+"",
                   action: "deactive"
               },
               function(data){
                   $the.attr("disabled", true);
                   $btn.attr("disabled", true);                   
                   $the.val(data);
               }
              );        
    }

}

function set_warn(id, phone, wkey){
    $the = $("#the_"+id+"_"+phone+"_"+wkey);
    $theval = $the.val();
    if($theval == ""){
        return;
    }
    
    $btn = $("#btn_"+id+"_"+phone+"_"+wkey);
    $btn.attr("disabled", true);
    $btnval = $btn.val();

    $btn.val("修改中....");
    
    $.post("do_mod",
           {
               devid: ""+id+"",
               warn: ""+wkey+"",
               warnval: $theval,
               mobile: ""+phone+"",
               action: "modify"
           },
           function(data){
               // $the.removeAttr("disabled");
               $btn.removeAttr("disabled");
               $btn.val($btnval);
           }
          );
}

function dev_mobile(id){
    $("#dev-mobile").html("<span style='color:red'>loading...</span>");
    $.post("mod_dev",
           {
               devid: ""+id+""
           },
           function(data){
               $(".list ul").children().css(unselected_style);               
               $("#item-"+id).css(selected_style);
               $("#dev-mobile").html(data).show("slow");
               $(".hidden").hide();
           }
          );
}

function add_mobile(id){
    $("#dev-mobile").html("<span style='color:red'>loading...</span>");    
    $.post("add_mobile",
           {
               devid: ""+id+""
           },
           function(data){
               $(".list ul").children().css(unselected_style);               
               $("#item-"+id).css(selected_style);
               $("#dev-mobile").html(data).show("slow");
           }
          );    
}

function new_mobile(id){
    phone = $("#mobile-item").val();
    if(phone == "") return false;
    
    $.post("new_mobile",
           {
               devid: ""+id+"",
               mobile: ""+phone+""
           },
           function(data){
               dev_mobile(id);
           }
          );
}

function add_dev(){
    devid_v = $("#devid").val();
    user_v = $("#userid").val();
    mob_v = $("#mobid").val();
    $.post("do_add_dev",
           {
               devid: ""+devid_v+"",
               username: ""+user_v+"",
               mobile: ""+mob_v+""
           },
           function(data){
               $("#devid").val("");
               $("#result").html(data).show("slow");
           }
          )
}

function show_box(boxid){
    $box = $("#box"+boxid);
    $(".hidden").hide();
    // $("#"+boxid).css("visibility","visible");
    $box.show();
}

function delete_box(boxid, devid){
    $box = $("#li"+boxid);
    $.post("delete_mobile",
           {
               "mobile": ""+ boxid+"",
               "devid": ""+ devid+ ""
           },
           function(data){
               $box.html(data)
                $box.fadeOut(2000);
           }
          );
}